package org.danger.t7.common.ws.cloud;

import org.danger.t7.common.ws.FkWebSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnClass(StringRedisTemplate.class)
public class WsRedisReceiver {
    private static final Logger logger = LoggerFactory.getLogger(WsRedisReceiver.class);
    private ApplicationContext applicationContext;

    public WsRedisReceiver(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }


    /**
     * 此方法会被反射调用
     */
    public void receiveMessage(String message) {
        logger.info("redis订阅监听到消息：{}", message);
        String[] parts = message.split(WsRedisBus.SPLITER);
        String fromUid = parts[0];
        String toUid = parts[1];
        String msg = message.replace(fromUid + WsRedisBus.SPLITER + toUid + WsRedisBus.SPLITER, "");
        FkWebSocket.sendMessageInThisBroker(fromUid, toUid, msg);
    }
}